local hongfa = fk.CreateSkill{
  name = "pegasus__hongfa",
}

local prompts = {}
prompts.damage = "#pegasus__hongfa-damage"
prompts.acquire = "#pegasus__hongfa-skill"

Fk:loadTranslationTable{
  ["pegasus__hongfa"] = "弘法",
  [":pegasus__hongfa"] = "有角色受到属性伤害时，你可以令此伤害改为于相同体力的角色间传导，结算后，受伤角色可以获得带有“限定技”标签的“问道”。",

  [prompts.damage] = "弘法：令此伤害改为于相同体力的角色间传导",
  [prompts.acquire] = "弘法：可以获得带有限定技标签的“问道”",
}

hongfa:addEffect(fk.DamageInflicted, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hongfa.name) and Fk:canChain(data.damageType) and not data.chain
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = hongfa.name, prompt = prompts.damage })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.beginnerOfTheDamage = true
    data.extra_data = data.extra_data or {}
    data.extra_data.pegasus__hongfa = true
    local targets = table.filter(room:getOtherPlayers(data.to), function (p)
      return p.hp == data.to.hp
    end)

    room.logic:getCurrentEvent():findParent(GameEvent.Damage, true):addExitFunc(function ()
      for _, p in ipairs(targets) do
        room:sendLog{
          type = "#ChainDamage",
          from = p.id,
        }

        room:damage{
          from = data.from,
          to = p,
          damage = data.damage,
          damageType = data.damageType,
          card = data.card,
          skillName = data.skillName,
          chain = true,
          extra_data = {
            pegasus__hongfa = true,
          }
        }
      end
    end)
  end,
})

local getWendao = function (player)
  local index = player.tag["pegasus__wendao"] or 1
  player.room:handleAddLoseSkills(player, "pegasus__wendao"..tostring(index))
  player.tag["pegasus__wendao"] = index + 1
end

hongfa:addEffect(fk.DamageFinished, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(hongfa.name) and data.extra_data and data.extra_data.pegasus__hongfa and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(data.to, { skill_name = hongfa.name, prompt = prompts.acquire })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:doIndicate(player, {data.to})
    getWendao(data.to)
  end,
})

return hongfa